<?php
/**
 * Create by Phpstorm
 * User: Peco
 * Date: 2019/12/14
 * Time: 10:21
 */

namespace App\Http\Controllers\Api;

use App\Models\Attachment;
use App\Repositories\AttachmentRepository;
use App\Http\Transformers\AttachmentTransformer;
use Illuminate\Http\Request;

/**
 * Class AttachmentController
 * @package App\Http\Controllers\Api\V1
 */
class AttachmentController extends BaseAdminController
{
    /**
     * @var AttachmentRepository
     */
    protected $attachmentRepository;

    /**
     * AttachmentController constructor.
     * @param $attachmentRepository
     */
    public function __construct(AttachmentRepository $attachmentRepository)
    {
        $this->attachmentRepository = $attachmentRepository;
    }

    /**
     * 保存附件
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function store(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'object_key' => 'required|string',
            'file_name' => 'string',
            'belong' => 'string',
            'belong_id' => 'integer',
        ], [], $this->attachmentRepository->attributes);

        if ($validator->fails()) {
            $this->errorBadRequest($validator);
        }

        if (!$result = $this->attachmentRepository->store($request->all())) {
            $this->response->errorInternal('创建失败');
        }
        return $this->response->item($result, new AttachmentTransformer())
            ->setStatusCode(201);
    }

    /**
     * 显示附件详情
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function show(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'id' => 'required|integer',
        ], [], $this->attachmentRepository->attributes);

        if ($validator->fails()) {
            $this->errorBadRequest($validator);
        }

        if (!$attachment = $this->attachmentRepository->show($request->get('id'))) {
            $this->response->errorNotFound();
        }
        return $this->response->item($attachment, new AttachmentTransformer());
    }

    /**
     * 删除附件
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function destroy(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'id' => 'required|integer',
        ], [], $this->attachmentRepository->attributes);

        if ($validator->fails()) {
            $this->errorBadRequest($validator);
        }
        if (!$result = $this->attachmentRepository->destroy($request->get('id'))) {
            $this->response->errorInternal('删除失败');
        }
        return $this->response->accepted();
    }

    /**
     * 附件更新
     * @param Request $request
     * @param $id
     */
    public function update(Request $request, $id)
    {
        $attach = Attachment::find($id);

        $attach->update($request->only([
            'document_id',
            'duration',
            'views'
        ]));
    }
}
